package com.example.testdemo.kinetic.sprite.tank.enemy;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import android.util.Log;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.kinetic.interfaces.IEnergyStorable;
import com.example.testdemo.kinetic.interfaces.IOnDestroyListener;
import com.example.testdemo.pools.TankPools;

public class EnemyTank1 extends EnemyBaseTank {
	public final static String BASE_NAME = EnemyBaseTank.BASE_NAME + " - "
	        + EnemyTank1.class.getSimpleName();

	private final String TAG = BASE_NAME;

	public static final float FACE_UP = 0;
	public static final float FACE_DOWN = 180;
	public static final float FACE_RIGHT = 90;
	public static final float FACE_LEFT = 270;

	public interface Animated {
		public static final int WALK_START = 0;
		public static final int WALK_END = 1;
		public static final int FIRE_START = 2;
		public static final int FIRE_END = 4;
		public static final int BOMB_START = 5;
		public static final int BOMB_END = 7;
	}

	private boolean mIsAnimationCancelable = true;

	public EnemyTank1(int whichScene, String pName, float pX, float pY,
	        ITiledTextureRegion pTiledTextureRegion, TankGameActivity pActivity) {
		super(whichScene, pName, pX, pY, pTiledTextureRegion, pActivity);
	}

	@Override
	protected void walkingAnimation() {
		if (mIsAnimationCancelable) {
			animate(new long[] { 100, 100 }, Animated.WALK_START,
			        Animated.WALK_END, true);
		}
	}

	@Override
	protected void fireAnimation() {
		if (mIsAnimationCancelable) {
			animate(new long[] { 20, 20, 30 }, Animated.FIRE_START,
			        Animated.FIRE_END, false,
			        new AnimatedSprite.IAnimationListener() {
				        @Override
				        public void onAnimationStarted(
				                AnimatedSprite pAnimatedSprite,
				                int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFrameChanged(
				                AnimatedSprite pAnimatedSprite,
				                int pOldFrameIndex, int pNewFrameIndex) {
				        }

				        @Override
				        public void onAnimationLoopFinished(
				                AnimatedSprite pAnimatedSprite,
				                int pRemainingLoopCount, int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFinished(
				                AnimatedSprite pAnimatedSprite) {
					        walkingAnimation();
				        }
			        });
		}
	}

	@Override
	protected void destroyedAnimation(final IOnDestroyListener listener) {
		if (mIsAnimationCancelable) {
			mIsAnimationCancelable = false;
			animate(new long[] { 50, 50, 50 }, Animated.BOMB_START,
			        Animated.BOMB_END, false,
			        new AnimatedSprite.IAnimationListener() {
				        @Override
				        public void onAnimationStarted(
				                AnimatedSprite pAnimatedSprite,
				                int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFrameChanged(
				                AnimatedSprite pAnimatedSprite,
				                int pOldFrameIndex, int pNewFrameIndex) {
				        }

				        @Override
				        public void onAnimationLoopFinished(
				                AnimatedSprite pAnimatedSprite,
				                int pRemainingLoopCount, int pInitialLoopCount) {
				        }

				        @Override
				        public void onAnimationFinished(
				                AnimatedSprite pAnimatedSprite) {
					        TankPools.getInstance(mWhichScene).recyle(EnemyTank1.this);
					        mIsAnimationCancelable = true;
					        if (listener != null) {
						        listener.onDestroy(EnemyTank1.this);
					        }
				        }
			        });
		}
	}

	@Override
	public void free() {
		mActivity.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {
				mOnScene.detachChild(EnemyTank1.this);
				dispose();
				Log.d(TAG, "dispose a " + TAG);
			}
		});
	}

	// IEnergyStorable

	@Override
	public void setEnergyLevel(int level) {
	}

	@Override
	public void levelUpEnergy() {
	}

	@Override
	public void clearEnergy() {
	}

	@Override
	public int getEnergyLevel() {
		return IEnergyStorable.MIN_LEVEL;
	}
}
